﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using AnguaRPGEngine.DND4eModule.Controls;
using AnguaRPGEngine.Library.Configuration;
using AnguaRPGEngine.Library.Systems;

namespace AnguaRPGEngine.DND4eModule.Forms
{
    public partial class GMPlayersForm : Form
    {
        private List<DNDPlayerCharacter> _PlayerCharacters;
        private Rectangle _ParentRect = new Rectangle();
        public GMPlayersForm(List<DNDPlayerCharacter> playerCharacters, Form parent)
        {
            _ParentRect = new Rectangle(parent.Left, parent.Top, parent.Width, parent.Height);
            InitializeComponent();
            this._PlayerCharacters = playerCharacters;
        }
        private void GMPlayersForm_Load(object sender, EventArgs e)
        {

            FormPosition fp = null;
            try
            {
                fp = GameSystem.Instance.ClientConfig.FormPositions.Find(p => p.FormName == this.Name);
            }
            catch
            {
                fp = null;
            }
            if (fp == null)
            {
                this.Left = 0;
                this.Top = _ParentRect.Top + _ParentRect.Height + 30;
                fp = new FormPosition();
                fp.FormName = this.Name;
                fp.Location = this.Location;
                fp.Size = this.Size;
                fp.Visible = this.Visible;
                GameSystem.Instance.ClientConfig.FormPositions.Add(fp);
            }
            else
            {
                this.Location = fp.Location;
                this.Size = fp.Size;
                this.Visible = fp.Visible;
            } 
            DisplayCharacters();
        }

        private void DisplayCharacters()
        {
            tabControl1.TabPages.Clear();
            foreach (DNDPlayerCharacter pc in _PlayerCharacters)
            {
                tabControl1.TabPages.Add(pc.CharacterName);
                PCInfoControl pcInfo = new PCInfoControl();
                pcInfo.SetCharacter(pc);
                pcInfo.Dock = DockStyle.Fill;
                tabControl1.TabPages[tabControl1.TabPages.Count - 1].Controls.Add(pcInfo);
            }
        }
        public void UpdateCharacter(DNDPlayerCharacter playerCharacter)
        {
            if (_PlayerCharacters.FindAll(p => p.CharacterName == playerCharacter.CharacterName).Count > 0)
            {
                DNDPlayerCharacter pc = _PlayerCharacters.Find(p => p.CharacterName == playerCharacter.CharacterName);
                pc.Update(playerCharacter);
            }
            else
            {
            }
        }
        private void GMPlayersForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!GameSystem.Instance.IsShuttingDown)
            {

                e.Cancel = true;
                this.Visible = false;
            }
            else
            {
                FormPosition fp = null;
                try
                {
                    fp = GameSystem.Instance.ClientConfig.FormPositions.Find(p => p.FormName == this.Name);
                }
                catch
                {
                    fp = null;
                }
                if (fp == null)
                {

                    fp = new FormPosition();
                    fp.FormName = this.Name;
                    fp.Location = this.Location;
                    fp.Size = this.Size;
                    fp.Visible = this.Visible;
                    GameSystem.Instance.ClientConfig.FormPositions.Add(fp);
                }
                else
                {
                    fp.Location = this.Location;
                    fp.Size = this.Size;
                    fp.Visible = this.Visible;
                }
            }
        }
    }
}
